﻿using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportingManagement.Components.Queries
{
    /// <summary>
    /// Interaction logic for QueryParametersCreator.xaml
    /// </summary>
    public partial class QueryParametersCreator : Window
    {
        private List<QueryParameter> parameters;

        public QueryParametersCreator()
        {
            InitializeComponent();
        }

        public QueryParametersCreator(List<QueryParameter> queryParameter)
        {
            InitializeComponent();

            this.parameters = queryParameter;
            lstParameters.ItemsSource = parameters;
        }

        public List<QueryParameter> QueryParameters
        {
            get
            {
                return this.parameters;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < lstParameters.Items.Count; i++)
            {
                ListBoxItem item = lstParameters.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (item != null)
                {
                    ContentPresenter contentPresent = FindVisualChild<ContentPresenter>(item);
                    DataTemplate dataTemplate = contentPresent.ContentTemplate;

                    TextBox textBox = (TextBox)dataTemplate.FindName("txtDescription", contentPresent);
                    if (string.IsNullOrWhiteSpace(textBox.Text))
                    {
                        MessageBox.Show("Parameter Description can not be empty!");
                        textBox.Focus();
                        return;
                    }

                    textBox = (TextBox)dataTemplate.FindName("txtValidationExpression", contentPresent);
                    if (string.IsNullOrWhiteSpace(textBox.Text))
                    {
                        MessageBox.Show("Parameter Validation Expression can not be empty!");
                        textBox.Focus();
                        return;
                    }

                    textBox = (TextBox)dataTemplate.FindName("txtDataTypeName", contentPresent);
                    if (string.IsNullOrWhiteSpace(textBox.Text))
                    {
                        MessageBox.Show("Parameter Data Type can not be empty!");
                        textBox.Focus();
                        return;
                    }
                }
            }

            this.DialogResult = true;
            this.Close();
        }

        public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T) return (T)child;
                else
                {
                    T childOfChild = FindVisualChild<T>(child);
                    if (childOfChild != null) return childOfChild;
                }
            }
            return null;
        }
    }
}
